package exttable.timetable.groupshchemapanel;

import dbutils.DbUtils;
import dbutils.StoredQueries;
import utils.Localisation;

import javax.swing.table.AbstractTableModel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 20.09.2010
 * Time: 23:32:48
 * To change this template use File | Settings | File Templates.
 */
public class FillGroupsInSchemaTableModel extends AbstractTableModel {

    private Vector<Vector> data = new Vector<Vector>();
    String[] columnNames = {Localisation.getMessage("table.column.shcema")
            , Localisation.getMessage("table.column.schemadescription")
            , Localisation.getMessage("table.column.schemagroup")
            , Localisation.getMessage("table.column.inverstype")
            , Localisation.getMessage("table.column.groupcode")
    };

    public FillGroupsInSchemaTableModel() {
        init();
    }

    private void init() {
        try {
            Connection c = DbUtils.getDBConnectionGeneral();
            PreparedStatement ps = c.prepareStatement(StoredQueries.getQuery(StoredQueries.FILL_GROUPS_IN_SCHEMAS));
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Vector row = new Vector(4);
                row.add(rs.getString("shemaID"));
                row.add(rs.getString("schemaDescription"));
                row.add(rs.getString("groupsSchema"));
                row.add(rs.getString("investType"));
                row.add("make Selection");
                data.add(row);
            }
            ps.close();
            c.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
       Vector<Vector> vec = data.get(rowIndex);
        return vec.get(columnIndex);
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex==4;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        Vector<Object> vec = data.get(rowIndex);
        vec.setElementAt(aValue, columnIndex);
    }
}

